<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;
class ForgetpwdController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex()
    {
        
        return view('/home/user/forgetpwd');
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postQuestionupdate(Request $request)
    {
        // 取出token值
        $req = $request ->except('_token');
        $req = $request->all();
        // var_dump($req);
        // 查询数据库中是否有此用户的安全问题
        $info = DB::table('tbl_home_user')
            ->where('id',$req['id'])
            ->orWhere('phone',$req['id'])
            ->orwhere('email',$req['id'])
            ->first();
            // dd($info);
        // if($info)
        $safety = DB::table('tbl_safety')->where('uid',$info['id'])->first();
        // dd($safety);
        $uid = $info['id'];
        if(($req['question1'] == $safety['question1']) && ($req['key1'] == $safety['key1']) && ($req['question2'] == $safety['question2']) && ($req['key2'] == $safety['key2'])){
            return view('/home/user/setpass',['uid'=>$uid])->with('success',"安全问题验证通过，请继续修改密码");
        }else{
            return back() -> with('error','安全问题验证失败，请核对过再重试');
        }
    }

    public function postSetpwd(Request $request){

        // 取出token值
        $req = $request ->except('_token');
        $req = $request->all();
        if($req['password'] == $req['repassword']){
              // 密码哈希加密
            $req['password'] = Hash::make($req['password']);
              // 将新密码更新数据库
            $res = DB::table('tbl_home_user') -> where('id',$req['id']) ->update(['password'=>$req['password']]);
            if($res){
                // 更新数据库成功则跳转登录页面  让用户登录
                return view('/home/home/activatesuccess');
            }
        }else{
            // 更新数据库失败则跳转回问题验证页面重新验证
            return back('/home/user/forgetpwd')->with('error','修改失败，请重试');
        }
 
    }

}
